package Sever;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

/**
 * 
 * @author ADMIN
 */
public class User implements AppConstants {
	private String name = "abc";
	private String password;
	private int state;

	/** Creates a new instance of Uer */
	public User() {
	}

	public User(String name) {
		try {
			Class.forName(driverClass);
			Connection con = DriverManager.getConnection(dbURL, dbUser,
					dbPassword);
			PreparedStatement stm = con
					.prepareStatement("Select * from  User  where name = ?");
			stm.setString(1, name);
			ResultSet rs = stm.executeQuery();
			if (rs.next()) {
				this.name = name;
				setPassword(rs.getString("password"));
				setStatic(rs.getInt("state"));
			}
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getState() {
		return state;
	}

	public void setStatic(int state) {
		this.state = state;
	}

	public String toString() {
		return name;
	}

	public void setUser(User us) {
		this.name = us.name;
		this.password = us.password;
		this.state = us.state;
	}

	// ----------------------------------------
	public User Login(String name, String password) {
		User us = new User(name);
		if (us.getPassword() == null)
			return null;
		if (us.getPassword().equalsIgnoreCase(password))
			return us;
		return null;
	}

	public static Vector<String> getAllUserOnl() {
		Vector<String> iList = new Vector<String>(10, 10);
		try {
			Class.forName(driverClass);
			Connection con = DriverManager.getConnection(dbURL, dbUser,
					dbPassword);
			Statement stmt = con.createStatement();
			ResultSet rs = stmt
					.executeQuery("Select name from User where state=" + 1);

			while (rs.next()) {
				String us = rs.getString("name");
				iList.add(us);

			}
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return iList;
	}

	public static Vector<String> getallScore() {
		Vector<String> iList = new Vector<String>(1, 2);
		try {
			Class.forName(driverClass);
			Connection con = DriverManager.getConnection(dbURL, dbUser,
					dbPassword);
			Statement stmt = con.createStatement();
			ResultSet rs = stmt
					.executeQuery("Select * from ketqua  ORDER BY score desc");
			int i = 0;
			while (rs.next()) {
				i++;
				String us = rs.getString("User");
				int dem = rs.getInt("score");
				iList.add(i + ": " + us + "- score: " + dem);

			}
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return iList;
	}

	public boolean updateOnl(int state) {
		try {
			Class.forName(driverClass);
			Connection con = DriverManager.getConnection(dbURL, dbUser,
					dbPassword);
			Statement stmt = con.createStatement();
			String sql = "Update User set state=" + state + " where name='"
					+ this.name + "'";
			int rc = stmt.executeUpdate(sql);
			if (rc == 1) {
				ResultSet rs = stmt
						.executeQuery("Select name from User where state="
								+ state + " and name='" + this.name + "'");
				if (rs.next())
					return true;
			}
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean updateScore(int score) {
		try {
			int d = 10000;
			Class.forName(driverClass);
			Connection con = DriverManager.getConnection(dbURL, dbUser,
					dbPassword);
			Statement stmt = con.createStatement();
			ResultSet rs = stmt
					.executeQuery("Select score from ketqua where User='"
							+ this.name + "'");
			if (rs.next()) {
				d = rs.getInt("score");
			}
			if (score > d) {
				String sql = "Update ketqua set score=" + score
						+ " where User='" + this.name + "'";
				int rc = stmt.executeUpdate(sql);
				if (rc == 1) {
					ResultSet rs1 = stmt
							.executeQuery("Select * from ketqua where score="
									+ score + " and name='" + this.name + "'");
					if (rs1.next())
						return true;
				}
			}
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static void main(String args[]) {
		User us = new User("abc");
		System.out.print(us.Login("abc", "1").getPassword());
	}
}
